package com.example.vpdemo;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.Button;
import android.widget.ProgressBar;

import androidx.core.widget.ContentLoadingProgressBar;

public class MyTask extends AsyncTask<Integer, Integer, String> {
//    @Override
//    protected String doInBackground(Integer... integers) {
//        return null;
//    }


    Button button;
    ContentLoadingProgressBar progressBar;

    public MyTask(Button button, ContentLoadingProgressBar progressBar) {
        this.button = button;
        this.progressBar = progressBar;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        button.setText("开始执行~");
    }


    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        Log.d("onProgressUpdate  long ",""+values[0]);
        progressBar.setProgress(values[0]);
        button.setText(""+values[0]);
    }

    @Override
    protected String doInBackground(Integer... integers) {
        try {
            int count = 0;
            int length = 1;
            while (count<100) {

                count += length;
                // 可调用publishProgress（）显示进度, 之后将执行onProgressUpdate（）
                publishProgress(count);
                Log.d("onProgressUpdate long",""+count);
                // 模拟耗时任务
                Thread.sleep(50);
            }
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }
}
